/**
 * <pre>
 * Title: 		LinksServiceImpl.java
 * Project: 	itfinoss
 * Author:		pengfangliang
 * Create:	 	2012-12-19 下午06:19:10
 * Copyright: 	Copyright (c) 2012
 * Company:		Shenzhen Helper
 * <pre>
 */
package com.hengpeng.itfinoss.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.hibernate.annotations.common.util.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.entity.AbstractEntity;
import com.hengpeng.common.persist.entity.AbstractEntity.BoolValue;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.model.Order.OrderType;
import com.hengpeng.itfinbase.persist.Links;
import com.hengpeng.itfinbase.persist.Links.LinkType;
import com.hengpeng.itfinbase.persist.Links.WebType;
import com.hengpeng.itfinoss.service.LinksService;
import com.hengpeng.itfinoss.service.ServiceFactory;

/**
 * <pre>
 * TODO 输入类型说明
 * </pre>
 * @author pengfangliang
 * @version 1.0, 2012-12-19
 */
@Service
public class LinksServiceImpl implements LinksService
{
	@Autowired
	private ServiceFactory serviceFactory;

	/*
	 * （非 Javadoc）
	 * @see com.hengpeng.itfinoss.service.LinksService#delete(java.util.Set)
	 */
	@Override
	public void delete(Set<Object> ids)
	{
		for (Object id : ids)
		{
			serviceFactory.getPersistService().delete(Links.class, id);
		}
	}
	
	@Override
	public <T extends AbstractEntity> ServiceResult<T> batchRemove(String field, Object[] values, Class<T> clz)
	{
		
		List<T> list = this.findListByField(field, values, clz).getReturnValue();
		for (T t : list)
		{
			serviceFactory.getPersistService().delete(clz, t.getId());
		}
		return new ServiceResult<T>();
	}
	
	public <T extends AbstractEntity> ServiceResult<List<T>> findListByField(String field, Object[] values, Class<T> clz){
		DynamicQuery query = new DynamicQuery(clz);
		query.inArray(field, values);
		return new ServiceResult<List<T>>(serviceFactory.getPersistService().find(clz, query));
	}

	@Override
	public ServiceResult<List<Links>> findLinksList(Integer pageIndex, Integer pageSize, Map<String, OrderType> sortMap, 
			String isPass, String name, String webType, String type) 
	{
		DynamicQuery query = new DynamicQuery(Links.class);
		query.setIsSearchTotalCount(true);
		query.setPageIndex(pageIndex);
		query.setPageSize(pageSize);
		//设置查询条件
		if(!StringHelper.isEmpty(isPass)) {
			query.eq("isPass", BoolValue.valueOf(isPass));
		}
		if(!StringHelper.isEmpty(name)) {
			query.like("name", "%" + name + "%");
		}
		if(!StringHelper.isEmpty(webType)) {
			query.eq("webType", WebType.valueOf(webType));
		}
		if(!StringHelper.isEmpty(type)) {
			query.eq("type", LinkType.valueOf(type));
		}
		//设置排序规则
		if (sortMap != null)
		{
			for (Entry<String, OrderType> entry : sortMap.entrySet())
			{
				if (entry.getValue() == OrderType.DESC)
				{
					query.desc(entry.getKey());
				}
				else
				{
					query.asc(entry.getKey());
				}
			}
		}
		//查询数据
		QueryResult<Links> queryResult = serviceFactory.getPersistService().findWithPage(Links.class, query);
		//返回值
		ServiceResult<List<Links>> serviceResult = new ServiceResult<List<Links>>();
		serviceResult.setReturnValue(queryResult.getResults());
		serviceResult.setReturnListSize((int) queryResult.getTotalCount());
		return serviceResult;
	}
}
